<?php

namespace app\common\model;


use app\common\lib\Status;
use think\Exception;

class MallCart extends Base
{


    protected $deleteTime = "delete_time";

    /**
     * @param string $openid
     * @param int $sku_id
     * @return MallCart|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMallCartBySkuIdAndOpenidInfo(string $openid = '', int $sku_id = 0)
    {
        return $this->where('openid', $openid)
            ->where('option_id', $sku_id)
            ->find();
    }

    /**
     * 获取购物车列表
     * @param string $openid
     * @param int $shop_id
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMallCartByOpenidInfo(string $openid = '',int $shop_id = 0): array
    {
        //库存为0的不展示
        try {
            $row = $this->where('openid', $openid)
                ->where('shop_id',$shop_id)
                ->with(['goodsSku', 'goods', 'shop'])
                ->order('id desc')
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("订单错误");
        }
        if (empty($row)) {
            return [];
        }
        $lists = [];
        $none_lists = [];
        foreach ($row as $v) {
            //查询规格
            $specsValue = (new MallSpecsValue())->getSpecsValueStringIds($v['goodsSku']['specs_value_ids']);
            $res_data = [
                "title" => $v['goods']['title'],
                "attr_val" => $specsValue,
                "image" => $v['goodsSku']['image'],
                "stock" => $v['goodsSku']['stock'],
                "price" => $v['goodsSku']['price'],
                "number" => $v['total'],
                "discount" => 0,
                "id" => $v['id']
            ];
            if ($v['goodsSku']['stock'] == 0) {
                $none_lists[$v['shop_id']]['list'][] = $res_data;
                $none_lists[$v['shop_id']]['shop_name'] = $v['shop']['title'];
                $none_lists[$v['shop_id']]['logo'] = $v['shop']['logo'];
            } else {
                //
                $lists[$v['shop_id']]['list'][] = $res_data;
                $lists[$v['shop_id']]['shop_name'] = $v['shop']['title'];
                $lists[$v['shop_id']]['logo'] = $v['shop']['logo'];
            }
        }
        return [
            "lists" => array_values($lists),
            "none_list" => array_values($none_lists)
        ];
    }

    /**
     * 查询商品sku
     * @return \think\model\relation\HasOne
     */
    public function goodsSku()
    {
        return $this->hasOne(MallGoodsSku::class, 'id', 'option_id');
    }

    /**
     * 查询商品
     * @return \think\model\relation\HasOne
     */
    public function goods()
    {
        return $this->hasOne(MallGoods::class, 'id', 'goods_id');
    }

    /**
     * 店铺
     * @return \think\model\relation\HasOne
     */
    public function shop()
    {
        return $this->hasOne(MallShop::class, 'id', 'shop_id');
    }

    /**
     * 查询购物车商品
     * @param array $ids
     * @param string $openid
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCartByIds(array $ids = [], string $openid = ''): array
    {
        $row = $this->where('openid', $openid)
            ->whereIn('id', $ids)
            ->with(['goods', 'goodsSku', 'goods'])
            ->select()
            ->toArray();
        if (empty($row)) {
            throw new Exception("不存在商品");
        }
        $lists = [];
        foreach ($row as $v){
            if($v['total'] > $v['goodsSku']['stock']){
                continue;
            }
            if ($v['goods_price'] != $v['goodsSku']['price']) {
                continue;
            }
            $lists[] = $v;
        }
        return $lists;
    }
}